package com.ruoyi.car.business.service.impl;


import com.ruoyi.car.business.domain.Appointment;
import com.ruoyi.car.business.domain.Statement;
import com.ruoyi.car.business.mapper.StatementMapper;
import com.ruoyi.car.business.query.StatementQuery;
import com.ruoyi.car.business.service.IAppointmentService;
import com.ruoyi.car.business.service.IStatementItemService;
import com.ruoyi.car.business.service.IStatementService;
import com.ruoyi.car.business.util.NewUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TablePageInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;

    @Autowired
    private IStatementItemService statementItemService;

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        Statement newStatement = new NewUtil().getNewStatement(statement);
        newStatement.setCreateTime(new Date());
        statementMapper.insert(newStatement);


    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        Statement newStatement = this.get(statement.getId());
        if (Statement.STATUS_PAID.equals(newStatement.getStatus())) {
            throw new GlobalException("非法操作");
        }
        newStatement.setCustomerName(statement.getCustomerName());
        newStatement.setCustomerPhone(statement.getCustomerPhone());
        newStatement.setActualArrivalTime(statement.getActualArrivalTime());
        newStatement.setLicensePlate(statement.getLicensePlate());
        newStatement.setCarSeries(statement.getCarSeries());
        newStatement.setServiceType(statement.getServiceType());
        newStatement.setInfo(statement.getInfo());
        statementMapper.updateByPrimaryKey(newStatement);
    }

    @Override
    @Transactional
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            // 先获取statement在进行删除
            Statement statement = this.get(dictId);
            // 判断是否是已支付 已支付不能删除
            if (Statement.STATUS_PAID.equals(statement.getStatus())) {
                throw new GlobalException("非法操作");
            }
            statementMapper.deleteByPrimaryKey(dictId);
            statementItemService.deleteByStatementId(dictId);
            if (statement.getAppointmentId()!=null) {
                // 修改预约单状态的状态为已到店
                appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_ARRIVAL);
            }
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void updateAmount(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal disCountPrice) {
        statementMapper.updateAmount(statementId,totalAmount,totalQuantity,disCountPrice);
    }

    @Override
    public void payStatement(Long statementId, Long userId, Integer status) {
        statementMapper.payStatement(statementId,userId,status);
    }

    @Override
    public Statement getByAppointmentId(Long appointmentId) {
        return statementMapper.getByAppointmentId(appointmentId);
    }

    @Override
    public void saveFromAppointment(Statement statement) {
        statementMapper.insert(statement);
    }

}
